<%-- 
    Document   : MessageDelete
    Created on : Dec 6, 2011, 8:44:27 PM
    Author     : hendrikde
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% if(session.getAttribute("LoggedIn")==null)
       {
    response.sendRedirect("index.html");
    return;
        }

%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Delete Message</title>
    </head>
    <body>
        <%
        int message_id = Integer.parseInt(request.getParameter("MESS"));
        int user_id = Integer.parseInt(session.getAttribute("UserID").toString());
        
        
       DBClasses.GetConnection gc = new DBClasses.GetConnection();
       java.sql.Connection conn = gc.getConnection();
        
       try {
           java.sql.Statement stmt = conn.createStatement();
           
           String query1 = "SELECT M.DeletedBy "
                   + "FROM Messages M "
                   + "WHERE M.MessageID=" +message_id;
           java.sql.ResultSet rs = stmt.executeQuery(query1);
           
           int deletedBy = -1;
           
           while(rs.next()) {
               deletedBy = Integer.parseInt(rs.getString(1));
           }
           
          if (deletedBy > -1) {
            conn.createStatement().execute("DELETE FROM Messages WHERE MessageID="+message_id);
          }
          else {
               conn.createStatement().execute("UPDATE MESSAGES M SET M.DeletedBy=" +user_id +" WHERE M.MessageID=" +message_id);
          }
       }
       catch (Exception e) {
           e.printStackTrace();
           response.sendRedirect("MessageIndex.jsp");
       }
       
       finally
    {
        try{
            conn.commit();
            conn.close();
            response.sendRedirect("MessageIndex.jsp");
        }
        catch(Exception ee)
                               {
            ee.printStackTrace();
        }
    }

       
        %>
    </body>
</html>
